{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "{'_id': ObjectId('5ab915065f04750a950109b6'), 'ip': '61.181.245.21', 'region': '中国 天津 天津 联通'}\n",
      "{'_id': ObjectId('5ab9152f5f04750a950109d6'), 'region': '联通'}\n",
      "{'_id': ObjectId('5aba62d05f04750a95010c1c'), 'ip': '58.22.248.12', 'region': '中国 福建 龙岩 联通'}\n",
      "{'_id': ObjectId('5abcfd2e5f04750a95011749'), 'ip': '175.16.221.237', 'region': '中国 吉林 吉林 联通'}\n"
     ]
    }
   ],
   "source": [
    "import pymongo\n",
    "import re\n",
    "\n",
    "client = pymongo.MongoClient('192.168.199.26:27017')\n",
    "db = client['dev_python']\n",
    "collection = db['proxy'] \n",
    "print(collection.find().count())\n",
    "\n",
    "#增\n",
    "#collection.insert({\"ip\":\"test2\", \"region\":'test2 中国 天津 天津 联通'})\n",
    "\n",
    "#删\n",
    "#collection.delete_many({'region':re.compile('电信')})\n",
    "collection.delete_one({'ip':'test2'})\n",
    "\n",
    "#改\n",
    "#collection.update_one({\"ip\":\"61.181.245.21\"},{\"$set\":{\"region\":'联通'}} )\n",
    "\n",
    "#查\n",
    "#itemList = collection.find({'protocol':'http'})\n",
    "#itemList = collection.find().sort(\"ip\", pymongo.ASCENDING)\n",
    "itemList = collection.find().sort([(\"ip\", pymongo.ASCENDING), (\"port\", pymongo.ASCENDING)]) \n",
    "itemList = collection.find({'region':re.compile('联通')},{'ip':1,'region':1})\n",
    "for item in itemList:\n",
    "    print(item)\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
